/*
 * Copyright (c) 2022-2022 All Rights Reserved.
 */

package com.driving.core.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.util.ConfigReader;
import com.driving.core.util.MessageType;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/test")
public class TestController {
    private final ConfigReader configReader;

    @GetMapping(value = { "", "{index}" })
    public Map<String, Object> test(HttpServletRequest request, @PathVariable(required = false) String index) {
        Map<String, String[]> param = request.getParameterMap();
        Map<String, Object> ret = new HashMap<>(param.size() + 4, 1);
        String suffix = StringUtils.isBlank(index) ? StringUtils.EMPTY : "-" + index;
        param.forEach((key, value) -> ret.put(key, Arrays.toString(value) + suffix));
        return ret;
    }

    // @GetMapping(value = "{index}")
    public Map<String, Object> index(HttpServletRequest request, @PathVariable String index) {
        Map<String, String[]> param = request.getParameterMap();
        Map<String, Object> ret = new HashMap<>(param.size() + 4, 1);
        param.forEach((key, value) -> ret.put(key, Arrays.toString(value) + "-" + index));
        return ret;
    }

    @GetMapping(value = "/test")
    public Map<String, String> test() {
        return Collections.singletonMap("message", configReader.getTestMessage(MessageType.TestType.LANGUAGE));
    }
}
